/**
 * 
 */
package bai6.com.client;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import bai6.com.myfile.MyFile;
import bai6.com.util.FileUtil;

/**
 * @author nglehien Oct 21, 2012 - 10:29:53 PM
 */
public class ClientGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8965799181938840452L;
	private JPanel contentPane;
	private JTextField txtPath;
	private JFileChooser fileChooser;
	private File fileToUpload;
	private JButton btnUpload;
	private JButton btnBrowse;
	private Socket socket;
	private JTextField txtFind;
	private JTextField txtReplace;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientGUI frame = new ClientGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClientGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 142);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		txtPath = new JTextField();
		txtPath.setBounds(10, 12, 301, 20);
		contentPane.add(txtPath);
		txtPath.setColumns(10);

		btnBrowse = new JButton("Browse");
		btnBrowse.addActionListener(onClickBrowse);
		btnBrowse.setBounds(335, 11, 89, 23);
		contentPane.add(btnBrowse);

		btnUpload = new JButton("Upload");
		btnUpload.addActionListener(onClickUpload);
		btnUpload.setBounds(114, 70, 89, 23);
		contentPane.add(btnUpload);

		txtFind = new JTextField();
		txtFind.setColumns(10);
		txtFind.setBounds(10, 39, 142, 20);
		contentPane.add(txtFind);

		txtReplace = new JTextField();
		txtReplace.setColumns(10);
		txtReplace.setBounds(162, 39, 149, 20);
		contentPane.add(txtReplace);
	}

	private ActionListener onClickUpload = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				socket = new Socket("localhost", 9999);
				ObjectOutputStream objectOutputStream = new ObjectOutputStream(
						socket.getOutputStream());
				String find = txtFind.getText();
				String replace = txtReplace.getText();
				MyFile myFile = new MyFile(fileToUpload, find, replace);

				objectOutputStream.writeObject(myFile);

				ObjectInputStream objectInputStream = new ObjectInputStream(
						socket.getInputStream());
				myFile = (MyFile) objectInputStream.readObject();
				String dst = fileToUpload.getParent() + "/replaced-"
						+ fileToUpload.getName();
				if (FileUtil.createNewFile(FileUtil.readFile(myFile.getFile())
						.toString(), dst)) {
					JOptionPane.showMessageDialog(ClientGUI.this, "Finish");
				} else {
					JOptionPane.showMessageDialog(ClientGUI.this, "Faild");
				}
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};

	private ActionListener onClickBrowse = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			fileChooser = new JFileChooser();
			int returnVal = fileChooser.showOpenDialog(ClientGUI.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				fileToUpload = fileChooser.getSelectedFile();
				txtPath.setText(fileToUpload.getAbsolutePath());
			}
		}
	};

}
